/******************************************/
/* DTD Inquisitor 2                       */
/* Copyright: University of Pennsylvania  */
/* Author: Byron Choi                     */
/* Date: 9/2001                           */
/* Contact: kkchoi@gradient.cis.upenn.edu */
/******************************************/
package automata;

import java.util.HashSet;
import java.util.Iterator;

/* StateSet is a set of States */
public class StateSet extends HashSet<State> implements Cloneable {

	private static final long serialVersionUID = 1L;

	public static StateSet empty() {
		return new StateSet();
	}

	public boolean add(State s) {
		return super.add(s);
	}

	public static StateSet singleton(State s) {
		StateSet ss = new StateSet();
		ss.add(s);
		return ss;
	}

	public static boolean is_singleton(StateSet ss) {
		return (ss.size() == 1);
	}

	public boolean exist(State s) {
		Iterator<State> i = iterator();
		while (i.hasNext()) {
			if (((State) (i.next())).equals(s)) {
				return true;
			}
		}
		return false;
	}

	public static StateSet union(StateSet ss1, StateSet ss2) {
		StateSet ss3 = StateSet.empty();
		Iterator<State> i1 = ss1.iterator();
		while (i1.hasNext()) {
			ss3.add(i1.next());
		}

		Iterator<State> i2 = ss2.iterator();
		while (i2.hasNext()) {
			ss3.add(i2.next());
		}
		return ss3;
	}

	public static StateSet minus(StateSet ss1, StateSet ss2) {
		StateSet ss3 = StateSet.empty();
		Iterator<State> i1 = ss1.iterator();
		while (i1.hasNext()) {
			State s = (State) (i1.next());
			if (!(ss2.exist(s))) {
				ss3.add(s);
			}
		}
		return ss3;
	}

	public static StateSet cross_product(StateSet ss1, StateSet ss2) {
		StateSet ss3 = StateSet.empty();
		Iterator<State> i1 = ss1.iterator();
		while (i1.hasNext()) {
			State s1 = (State) (i1.next());
			Iterator<State> i2 = ss2.iterator();
			while (i2.hasNext()) {
				State s2 = (State) (i2.next());
				StatePair s3 = new StatePair(s1.elt, s2.elt);
				ss3.add(s3);
			}
		}
		return ss3;
	}

	public String toString() {
		String str = new String();
		Iterator<State> i = this.iterator();
		while (i.hasNext()) {
			str += ((State) (i.next())).toString();
			if (i.hasNext()) {
				str += " ";
			}
		}
		return str;
	}

}
